/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista.Calibrar;

import Vista.configuracion.MenuPal;
import Vista.configuracion.SetButton;
import Vista.configuracion.Settings;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
/**
 *
 * @author Jhon Jairo
 */
public class Calibrar extends Thread {
    private Point pUl, pUr, pDl, pDr;
    private int count;
    private int rango = 300;
    private int anchoX;
    private int altoY;
    private int ResolucionX;
    private int ResolucionY;
    private JLabel jl1;
    private JLabel jl2;
    private JLabel jl3;
    private JLabel jl4;
    private boolean calibrar = true;
    public static Settings set = new Settings();
    public static MenuPal mnp = new MenuPal();
    public static SetButton sb = new SetButton();

    public Calibrar(JLabel jl1, JLabel jl2, JLabel jl3, JLabel jl4) {
        count = 0;
        pUl = new Point(0, 0);
        pUr = new Point(0, 0);
        pDl = new Point(0, 0);
        pDr = new Point(0, 0);
        this.jl1 = jl1;
        this.jl2 = jl2;
        this.jl3 = jl3;
        this.jl4 = jl4;
    }

    @Override
    public void run() {
        while (calibrar) {
            switch (count) {
                case 0:
                    jl1.setVisible(true);
                    jl2.setVisible(false);
                    jl3.setVisible(false);
                    jl4.setVisible(false);
                    break;
                case 1:
                    jl1.setVisible(false);
                    jl2.setVisible(true);
                    jl3.setVisible(false);
                    jl4.setVisible(false);
                    break;
                case 2:
                    jl1.setVisible(false);
                    jl2.setVisible(false);
                    jl3.setVisible(true);
                    jl4.setVisible(false);
                    break;
                case 3:
                    jl1.setVisible(false);
                    jl2.setVisible(false);
                    jl3.setVisible(false);
                    jl4.setVisible(true);
                    break;
                case 4:
                    jl1.setVisible(false);
                    jl2.setVisible(false);
                    jl3.setVisible(false);
                    jl4.setVisible(false);
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                Logger.getLogger(Calibrar.class.getName()).log(Level.SEVERE, null, ex);
            }
                    //set.setVisible(true);
                    sb.setVisible(true);
                    sb.setAlwaysOnTop(true);
                    sb.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width-(Toolkit.getDefaultToolkit().getScreenSize().width/2), 0);
                    CalibrateFrame.calibrateFrame.setVisible(false);
                    break;
            }
        }
    }

    public void setPoint(Point p) {
        //System.out.println("x= " + p.getX() + "y= " + p.getY() + "count=" + count);
        CalibrateFrame.jLabelInfo.setVisible(true);
        CalibrateFrame.jLabelInfo.setText("recibo Coordenadas wii: x="+ p.getX() + "y= " + p.getY());
        if (count == 0) {
          
            pUl = p;
            count++;
        } else if (count == 1 && pUl.getX() > p.getX() + rango) {
           
            pUr = p;
            count++;
        } else if (count == 2 && pUr.getY() > p.getY() + rango) {
            
            pDl = p;
            count++;
        } else if (count == 3 && pDl.getX() > p.getX() + rango) {
            pDr = p;
            count++;
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(Calibrar.class.getName()).log(Level.SEVERE, null, ex);
            }
            setCalibrationValues();
        }
    }

    public void setCalibrationValues() {
        anchoX = (int) (pUl.getX() - pUr.getX());
        altoY = (int) (pUl.getY() - pDl.getY());
        int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int heigth = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        ResolucionX = (int) Math.round(((double) pUl.getX() / anchoX) * width);
        ResolucionY = (int) Math.round(((double) pUl.getY() / altoY) * heigth);
        count=4;
        calibrar = false;
        System.out.println("calibrado");
    }

    public int getResolucionX() {
        return ResolucionX;
    }

    public int getResolucionY() {
        return ResolucionY;
    }

    public int getAltoY() {
        return altoY;
    }

    public int getAnchoX() {
        return anchoX;
    }

    public boolean isCalibrar() {
        return calibrar;
    }
}
